﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;
using System.Data;
using System.Collections;

namespace FilStartUps.Forum
{
    public partial class ShowMessage : System.Web.UI.Page
    {
        public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                {
                    return Convert.ToInt32(ViewState["PageNumber"]);
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                ViewState["PageNumber"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetMessageData();
            }
        }

        private void GetMessageData()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                //Query String == ForumThreadid
                int forumthreadid = int.Parse(Request.QueryString["t"].Trim());

                var forum = (from f in db_connect.Forums
                             join ft in db_connect.ForumThreads
                              on f.forumidPK equals ft.forumidFK
                             where ft.forumthreadidPK.Equals(forumthreadid)
                             select new
                             {
                                 forumidPK = f.forumidPK,
                                 threadtitle = ft.threadtitle,
                                 forumtitle = f.forumtitle
                             }).SingleOrDefault();

                forumlink.Text = forum.forumtitle;
                forumlink.NavigateUrl = "~/Forum/Thread.aspx?t=" + forum.forumidPK;
                lbthreadname.Text = forum.threadtitle;

                var msg = (from fm in db_connect.ForumMessages
                               where fm.forumthreadidFK.Equals(forumthreadid)
                               && fm.disabled.Equals(false)
                               select fm).ToList();

                //EnumerableRowCollection<DataRow> query = from msg_ in msg.AsEnumerable()
                //                                         where msg_.disabled.Equals(false)
                //                                         select msg_;
                    
                    //db_connect.ForumMessages.Where(fm => fm.forumthreadidFK.Equals(forumthreadid)
                    //   && fm.disabled.Equals(false)).ToList();
                //PagedDataSource pgitems = new PagedDataSource();
                //DataView dv = new DataView(msg);
                //pgitems.DataSource = msg;
                //pgitems.AllowPaging = true;
                //pgitems.PageSize = 3;
                //pgitems.CurrentPageIndex = PageNumber;
                //if (pgitems.PageCount > 1)
                //{
                //    rptPages.Visible = true;
                //    ArrayList pages = new ArrayList();
                //    for (int i = 0; i < pgitems.PageCount; i++)
                //        pages.Add((i + 1).ToString());
                //    rptPages.DataSource = pages;
                //    rptPages.DataBind();
                //}
                //else
                //{
                //    rptPages.Visible = false;
                //    rptMessagesList.DataSource = pgitems;
                //    rptMessagesList.DataBind();
                //}

                rptMessagesList.DataSource = msg;
                rptMessagesList.DataBind();
            }
        }

        protected void rptPages_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
            GetMessageData();
        }

        protected void rpMessagesList_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "complain")
            {
                Label lbmsgid = (Label)rptMessagesList.Items[e.Item.ItemIndex].FindControl("lbmsgid");
                //Response;
            }
        }

        protected void lbreply_Click(object sender, EventArgs e)
        {
            //ForumThreadidFK
            Response.Redirect("~/Forum/ThreadReply.aspx?id=" + Request.QueryString["t"].Trim());
        }
    }
}